package com.ruoyi.subject.mapper;

import com.ruoyi.subject.domain.QbExamAnalyse;
import com.ruoyi.subject.domain.QbExamRecord;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 考试记录Mapper接口
 *
 * @author lzhj
 * @date 2021-01-27
 */
public interface QbExamRecordMapper {
    /**
     * 查询考试记录
     *
     * @param id 考试记录ID
     * @return 考试记录
     */
    public QbExamRecord selectQbExamRecordById(Long id);

    /**
     * 查询考试记录列表
     *
     * @param qbExamRecord 考试记录
     * @return 考试记录集合
     */
    public List<QbExamRecord> selectQbExamRecordList(QbExamRecord qbExamRecord);

    /**
     * 新增考试记录
     *
     * @param qbExamRecord 考试记录
     * @return 结果
     */
    public int insertQbExamRecord(QbExamRecord qbExamRecord);

    /**
     * 修改考试记录
     *
     * @param qbExamRecord 考试记录
     * @return 结果
     */
    public int updateQbExamRecord(QbExamRecord qbExamRecord);

    /**
     * 删除考试记录
     *
     * @param id 考试记录ID
     * @return 结果
     */
    public int deleteQbExamRecordById(Long id);

    /**
     * 批量删除考试记录
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteQbExamRecordByIds(Long[] ids);

    /**
     * 查询成绩排名前100的人员及成绩
     *
     * @return
     */
    @ResultType(value = LinkedHashMap.class)
    @Select("SELECT s.id record_id,s.user_id,u.user_name,u.zjh,DATE_FORMAT(s.exam_time,'%Y-%m-%d %H:%i:%s') exam_time,DATE_FORMAT(s.end_time,'%Y-%m-%d %H:%i:%s') end_time ,s.sub_type,s.score,s.rank1\n" +
            "FROM\n" +
            "(\n" +
            "SELECT c.*,\n" +
            "IF(@p=c.sub_type,\n" +
            "    CASE \n" +
            "       WHEN @s=c.score THEN @r\n" +
            "       WHEN @s:=c.score THEN @r:=@r+1\n" +
            "    END,\n" +
            "   @r:=1 ) AS rank1,\n" +
            "@p:=c.sub_type,\n" +
            "@s:=c.score\n" +
            "FROM t_qb_exam_record c,(SELECT @p:=NULL,@s:=NULL,@r:=0)r\n" +
            "where  c.sub_type is not null \n" +
            "ORDER BY c.sub_type,c.score desc \n" +
            ")s  left join t_qb_user u on s.user_id=u.id\n" +
            "\n" +
            "WHERE s.rank1<=100 ORDER BY s.sub_type,s.rank1")
    List<Map<String, Object>> selectSorceRank();

    /**
     * 查询模拟考试分析
     * @param qbExamAnalyse
     * @return
     */
    List<QbExamAnalyse> selectExamAnalyseList(QbExamAnalyse qbExamAnalyse);
}
